package com.kaifamiao.provider;

import com.kaifamiao.model.AdminModel;
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;
import org.apache.ibatis.jdbc.SQL;

public class AdminProvider implements ProviderMethodResolver {

    public static String selectByPrimaryKey(final AdminModel model){
        return new SQL(){{
            SELECT("*");
            FROM("admin");
            if(model!=null) {
                if (model.getId() != null) {
                    WHERE("id=#{id}");
                }
            }
        }}.toString();
    }

    public static String updateByPrimaryKeySelective(final AdminModel model){
        return new SQL(){{
            UPDATE("admin");
            if(model!=null){
                if(model.getAdminname()!=null){
                    SET("adminName=#{adminName}");
                }
                if(model.getPassword()!=null){
                    SET("password=#{password}");
                }
                if(model.getRealname()!=null){
                    SET("realName=#{realName}");
                }
                if(model.getSex()!=null){
                    SET("sex=#{sex}");
                }
                if(model.getTouxiang()!=null){
                    SET("touxiang=#{touxiang}");
                }

                if(model.getTel()!=null){
                    SET("tel=#{tel}");
                }
                if(model.getRoleid()!=null){
                    SET("roleId=#{roleId}");
                }
                if(model.getId()!=null){
                    WHERE("id=#{id}");
                }
            }

        }}.toString();
    }

    public static String selectByAdmin(final AdminModel model) {
        return new SQL() {{
            SELECT("*");
            FROM("admin");
            if (model != null) {
                if(model.getAdminname()!=null){
                    WHERE("adminName=#{adminName}");
                }
                if(model.getPassword()!=null){
                    WHERE("password=#{password}");
                }
            }
        }}.toString();
    }

    public static String selectByAdminName(final String adminName){
        return new SQL(){{
            SELECT("*");
            FROM("admin");
            if(adminName != null){
                WHERE("adminName=#{adminName}");
            }
        }}.toString();
    }
}

